function tooltip(target_items, name){
    $(target_items).each(function(i){
        
        var id = $(this).attr('id');
        debugger;
        var result = jQuery.ajax({
            url: "./webservice.php",       
            cache: false,
            async: false,
            data: ({
                method: "getCharacter",
                id: id
            }),
            success: function(message) {
                var jObj = JSON.parse(message);
                return jObj;
            //$("#preloader").hide();
            },
            error: function(req,status,error) {
                debugger;
                jQuery("#content").empty().append(error);
                //$("#preloader").hide();
                alert(error);
            }
        });
        debugger;
        var character = JSON.parse(result.responseText);
        if(character.description != "")
            character.description = "Beschreibung:<br>"+character.description;
        else
            character.description = "";
        $("body").append("<div class='"+name+"' id='_"+id+"'><p>"+character.surname+" "+character.lastname+"<br><img src='./res/img_char/"+character.portrait+"'><br>"+character.description+"</p></div>");
        var my_tooltip = $("#_"+id);

        $(this).mouseover(function(){
            my_tooltip.animate({
                opacity:0.9
            },{
                queue:false
            });
        }).mousemove(function(kmouse){
            my_tooltip.css({
                left:kmouse.pageX+15, 
                top:kmouse.pageY+15
            });
        }).mouseout(function(){
            my_tooltip.animate({
                opacity:0
            },{
                duration:400,
                queue:false
            });
        });
    });
}

function getCharacterDetails(id){
    jQuery.ajax({
        cache: false,
        async: false,
        url: './webservice.php',
        data: ({
            method: "getDetails",
            id: id
        }),
        success: function(message) {
            return message;
        },
        error: function(req,status,error)
        {
            debugger;
            console.log("getCharacterDetails",error);
        }
    });
}

function removeCharacter(id)
{
    alert(deleteCharacter(id));
    loadTabContent('./view/admin.character.overview.php','');
}

function removeOrganisation(id)
{
    alert(deleteOrganisation(id));
    loadTabContent('./view/admin.organisation.overview.php','');
}

function deleteOrganisation(id){
    var result = $.ajax({
        type: 'GET',
        async: false,
        url: './webservice.php',
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        data: ({
            method: "deleteOrganisation",
            id: id
        }),
        success: function(result) {
            return result;
        },
        error: function(req,status,error)
        {
            debugger;
            console.log("deleteOrganisation",error);
        }
    });
    var success = JSON.parse(result.responseText);
    if(success.status == 200)
    {
        if(success.message == 1)
            return "Löschung erfolgreich";
        if(success.message == 0)
            return "Organisation existiert nicht";
        if(success.message == -1)
            return "Löschung fehlgeschlagen";
    }
    if(success.status == 500)
        return "Fehler: " + success.message;
}

function deleteCharacter(id){
    var result = $.ajax({
        type: 'GET',
        async: false,   // WICHTIG!
        url: './webservice.php',
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        data: ({
            method: "deleteCharacter",
            id: id
        }),
        success: function(result) {
            return result;
        },
        error: function(req,status,error)
        {
            debugger;
            console.log("deleteCharacter",error);
        }
    });
    var success = JSON.parse(result.responseText);
    if(success.status == 200)
    {
        if(success.message == 1)
            return "Löschung erfolgreich";
        if(success.message == 0)
            return "Charakter existiert nicht";
        if(success.message == -1)
            return "Löschung fehlgeschlagen";
    }
    if(success.status == 500)
        return "Fehler: " + success.message;
}

function addCharacter(character)
{
    var result = jQuery.ajax({
        type: 'GET',
        async: false,
        url: './webservice.php',
        data: ({
            method: "addCharacter",
            _cid: character._cid,
            _oid: character._oid,
            _rid: character._rid,
            surname: character.surname,
            lastname: character.lastname,
            age: character.age,
            gender: character.gender,
            race: character.race,
            portrait: character.portrait,
            status: character.status,
            profession: character.profession,
            type: character.type,
            task: character.task,
            klasse: character.klasse,
            description: character.description
        }),
        success: function(result) {
            debugger;
            return result;
        },
        error: function(req,status,error) {
            debugger;
            console.log("error");
            return error;
        }
    });
    var success = JSON.parse(result.responseText);
    debugger;
    if(success.status == 200)
    {
        if(success.message == 1)
            return "Charaktererstellung erfolgreich";
        if(success.message == 0)
            return "Vorname existiert bereits";
        if(success.message == -1)
            return "Charaktererstellung fehlgeschlagen";
    }
    if(success.status == 500)
        return "Fehler: " + success.message;
}

function updateCharacter(character)
{
    debugger;
    var result = jQuery.ajax({
        type: 'GET',
        async: false,
        url: './webservice.php',
        data: ({
            method: "updateCharacter",
            _id: character._id,
            _cid: character._cid,
            _oid: character._oid,
            _rid: character._rid,
            surname: character.surname,
            lastname: character.lastname,
            age: character.age,
            gender: character.gender,
            race: character.race,
            portrait: character.portrait,
            status: character.status,
            profession: character.profession,
            type: character.type,
            task: character.task,
            klasse: character.klasse,
            description: character.description
        }),
        success: function(result) {
            debugger;
            return result;
        },
        error: function(req,status,error) {
            debugger;
            console.log("error");
            return error;
        }
    });
    var success = JSON.parse(result.responseText);
    debugger;
    if(success.status == 200)
    {
        if(success.message == 1)
            return "Bearbeitung erfolgreich";
        if(success.message == 0)
            return "Es wurde nichts verändert";
        if(success.message == -1)
            return "Bearbeitung fehlgeschlagen";
    }
    if(success.status == 500)
        return "Fehler: " + success.message;
}

function addOrganisation(organisation)
{
    var result = jQuery.ajax({
        type: 'GET',
        async: false,
        url: './webservice.php',
        data: ({
            method: "addOrganisation",
            name: organisation.name,
            _oid: organisation._oid,
            _cid: organisation._cid,
            description: organisation.description
        }),
        success: function(result) {
            debugger;
            return result;
        },
        error: function(req,status,error) {
            debugger;
            console.log("error");
            return error;
        }
    });
    var success = JSON.parse(result.responseText);
    debugger;
    if(success.status == 200)
    {
        if(success.message == 1)
            return "Erstellung der Organisation erfolgreich";
        if(success.message == 0)
            return "Organisation existiert bereits";
        if(success.message == -1)
            return "Erstellung fehlgeschlagen";
    }
    if(success.status == 500)
        return "Fehler: " + success.message;
}

function addRank(rank)
{
    var result = jQuery.ajax({
        type: 'GET',
        async: false,
        url: './webservice.php',
        data: ({
            method: "addRank",
            name: rank.name,
            _oid: rank._oid,
            _rid: rank._rid
        }),
        success: function(result) {
            debugger;
            return result;
        },
        error: function(req,status,error) {
            debugger;
            console.log("error");
            return error;
        }
    });
    var success = JSON.parse(result.responseText);
    debugger;
    if(success.status == 200)
    {
        if(success.message == 1)
            return "Erstellung des Rangs erfolgreich";
        if(success.message == 0)
            return "Rang existiert bereits";
        if(success.message == -1)
            return "Erstellung fehlgeschlagen";
    }
    if(success.status == 500)
        return "Fehler: " + success.message;
}

function loadTabContent(tabUrl,args){
    $("#preloader").show();
    jQuery.ajax({
        url: tabUrl,       
        cache: false,
        async: false,
        data: ({
            type: args
        }),
        success: function(message) {
            jQuery("#tabcontent").empty().append(message);
            $("#preloader").hide();
        },
        error: function(req,status,error) {
            jQuery("#tabcontent").empty().append(error);
            $("#preloader").hide();
            alert(error);
        }
    });
    //$("#preloader").hide();
}